package com.oa.controller;

import com.oa.back.ResponseBack;
import com.oa.common.annotion.Interceptor;
import com.oa.common.base.Pageable;
import com.oa.common.enums.AppsType;
import com.oa.common.enums.AppsUserType;
import com.oa.common.enums.CodeStatus;
import com.oa.service.AppsTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/manage/appsType")
public class AppsTypeController extends BaseController {

    @Autowired
    private AppsTypeService appsTypeService;

    private static final Logger logger= LoggerFactory.getLogger(AppsTypeController.class);

    @RequestMapping(value = "index.html",method = RequestMethod.GET)
    public String index(ModelMap modelMap, Pageable pageable,String key,Integer type){
        modelMap.addAttribute("page",appsTypeService.findPage(pageable,key,type));
        modelMap.addAttribute("pageable",pageable);
        modelMap.addAttribute("key",key);
        modelMap.addAttribute("type",type);
        return "/admin/appstype/index";
    }

    @RequestMapping(value = "add.html",method = RequestMethod.GET)
    public String add(ModelMap modelMap){
        modelMap.addAttribute("types",AppsType.values());
        modelMap.addAttribute("uses",AppsUserType.values());
        return "/admin/appstype/add";
    }

    @RequestMapping(value = "submit",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "创建公文类型")
    public ResponseBack<String> submit(String params){
        try{
            return appsTypeService.submit(params);
        }catch (RuntimeException ex){
            logger.error("创建公文接口异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }

    @RequestMapping(value = "validate",method = RequestMethod.GET)
    @ResponseBody
    @Interceptor(actionName = "校验公文是否存在")
    public Boolean validate(String title,Long id){
        return !appsTypeService.validate(title, id);
    }


}
